#!/bin/env python
"""
	This script will generate a bunch of Addons that are LoadManager'd.
	Either by their own small loading addon or by AddonLoader.
"""
import os

USE_ADDONLOADER = False
COUNT = 2
ADDONS_PATH = 'AddOns'

class DictionaryTemplate:
	template = ''
	def __init__(self, dict={}, **keywords):
		self.dict = dict
		self.dict.update(keywords)
	def __str__(self):
		return self.template % self
	def __getitem__(self, key):
			return self.process(key.split("|"))
	def process(self, l):
		arg = l[0]
		if len(l) == 1:
			if arg in self.dict:
				return self.dict[arg]
			elif hasattr(self, arg) and callable(getattr(self, arg)):
				return getattr(self, arg)()
			else:
				raise KeyError(arg)
		else:
			func_name = l[1]
			if func_name in self.dict:
				func = self.dict[func_name]
			else:
				func = getattr(self, func_name)
			return func(self.process([arg]))

class AddOnGenerator:
	def __init__(self, name, toc_template, lua_template):
		self.dict = {
			'addon_name': name,
			'loader_name': name + "_Loader",
			'func_name': name + "_Load",
		}
		self.toc_template = toc_template(self.dict)
		self.lua_template = lua_template(self.dict)
	def __call__(self):
		if self.toc_template.loader:
			addon_name =  self.dict['loader_name']
		else:
			addon_name = self.dict['addon_name']
		addon_path = os.path.join(ADDONS_PATH, addon_name)
		os.mkdir(addon_path)
		toc_path = os.path.join(addon_path, addon_name + ".toc")
		toc = open(toc_path, "w")
		toc.write(str(self.toc_template))
		toc.close()
		lua_path = os.path.join(addon_path, addon_name + ".lua")
		lua = open(lua_path, "w")
		lua.write(str(self.lua_template))
		lua.close()

class LMAddon_TOC(DictionaryTemplate):
	loader = False
	template = """
## Interface: 30000
## LoadManagers: %(loader_name)s

%(addon_name)s.lua
"""

class LMAddon_lua(DictionaryTemplate):
	template = """
%(addon_name)s = {
	%(func_name)s = function ()
		%(func_name)s = nil
		%(addon_name)s = nil
	end
}
"""

class LMLoader_TOC(DictionaryTemplate):
	loader = True
	template = """
## Interface: 30000

%(loader_name)s.lua
"""

class LMLoader_lua(DictionaryTemplate):
	template = """
if IsAddOnLoaded("%(addon_name)s") then return end

local prev_%(func_name)s = %(func_name)s
%(func_name)s = function ()
	%(func_name)s = prev_%(func_name)s
	LoadAddOn("%(addon_name)s")
	local %(addon_name)s = %(addon_name)s
	if %(addon_name)s then
		%(addon_name)s:%(func_name)s()
	else
		prev_%(func_name)s()
	end
end
"""

class ALAddon_TOC(DictionaryTemplate):
	loader = False
	template = """
## Interface: 30000
## LoadManagers: AddonLoader
## X-LoadOn-Execute: local prev_%(func_name)s = %(func_name)s %(func_name)s = function ()
## X-LoadOn-Execute2: %(func_name)s = prev_%(func_name)s AddonLoader:LoadAddOn("%(addon_name)s")
## X-LoadOn-Execute3: local %(addon_name)s = %(addon_name)s if %(addon_name)s then
## X-LoadOn-Execute4: %(addon_name)s:%(func_name)s() else prev_%(func_name)s() end end
"""

def main():
	if USE_ADDONLOADER:
		for i in range(COUNT):
			AddOnGenerator('AL_Addon%d' % (i + 1), ALAddon_TOC, LMAddon_lua)()
	else:
		for i in range(COUNT):
			AddOnGenerator('LM_Addon%d' % (i + 1), LMAddon_TOC, LMAddon_lua)()
			AddOnGenerator('LM_Addon%d' % (i + 1), LMLoader_TOC, LMLoader_lua)()

main()
